package com.kyrioscraft.cloud.security.securitybrowser.session;

import com.kyrioscraft.cloud.security.core.validate.code.ValidateCode;
import com.kyrioscraft.cloud.security.core.validate.code.ValidateCodeRepository;
import com.kyrioscraft.cloud.security.core.validate.code.ValidateCodeType;

import org.apache.commons.lang3.StringUtils;
import org.springframework.social.connect.web.HttpSessionSessionStrategy;
import org.springframework.social.connect.web.SessionStrategy;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * @Author kyrioscraft
 * @Date 2019/1/8 15:34
 * @Description //TODO
 **/
@Component
public class SessionValidateCodeRepository implements ValidateCodeRepository {
    String SESSION_KEY_PREFIX = "SESSION_KEY_FOR_CODE_";
    private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();

    @Override
    public void save(ServletWebRequest request, ValidateCode code, ValidateCodeType validateCodeType) {
        sessionStrategy.setAttribute(request, getSessionKey(request,validateCodeType),code);
    }

    @Override
    public ValidateCode get(ServletWebRequest request, ValidateCodeType validateCodeType) {
        return (ValidateCode)sessionStrategy.getAttribute(request, getSessionKey(request,validateCodeType));
    }

    @Override
    public void remove(ServletWebRequest request, ValidateCodeType validateCodeType) {
        sessionStrategy.removeAttribute(request,getSessionKey(request,validateCodeType));
    }

    private String getSessionKey(ServletWebRequest request, ValidateCodeType validateCodeType) {
        return SESSION_KEY_PREFIX + validateCodeType.toString().toUpperCase();
    }

}
